/*
*	Copyright © 2011, The Vibzworld Team
*	All rights reserved.
*	http://code.google.com/p/webinject/
*	
*	Redistribution and use in source and binary forms, with or without
*	modification, are permitted provided that the following conditions
*	are met:
*	
*	- Redistributions of source code must retain the above copyright
*	notice, this list of conditions and the following disclaimer.
*	
*	- Neither the name of the Vibzworld Team, nor the names of its
*	contributors may be used to endorse or promote products
*	derived from this software without specific prior written
*	permission.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using mshtml;

namespace WebInject
{
    public partial class Inject : Form
    {
        Queue<string> history = new Queue<string>();
        public Inject()
        {
            InitializeComponent();
            this.Text += " - v." + Assembly.GetExecutingAssembly().GetName().Version.ToString();
            wbPage.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wbPage_DocumentCompleted);
        }

        void wbPage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            rtbScript.Enabled = true;
            tsbAddScript.Enabled = true;
            
            if (history.Count != 0)
                tsbHistory.Enabled = true;
        }

        private void tsbAddScript_Click(object sender, EventArgs e)
        {
            ProcessScript(rtbScript.Text);        
        }
        void ProcessScript(string script)
        {
            if (history.Count == 10)
                history.Dequeue();
            history.Enqueue(script);
            InjectScript(script);
            tsbHistory.Enabled = true;
        }
        void InjectScript(string script)
        {
            if (script.Trim() != "")
            {
                //wbPage.Navigate("javascript:function __inject(){" + script + "} __inject();");
                HtmlElement head = wbPage.Document.GetElementsByTagName("head")[0];
                HtmlElement scriptEl = wbPage.Document.CreateElement("script"); 
                IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
                element.text = "function __inject() { " + script + " }"; 
                head.AppendChild(scriptEl);
                wbPage.Document.InvokeScript("__inject");  

            }
        }
        private void pbGo_Click(object sender, EventArgs e)
        {
            OpenUrl();
        }

        void OpenUrl()
        {
            rtbScript.Enabled = false;
            tsbAddScript.Enabled = false;
            tsbHistory.Enabled = false;
            if (txtUrl.Text.Trim() != "")
                wbPage.Navigate(txtUrl.Text);
        }
        private void tsbHistory_DropDownOpening(object sender, EventArgs e)
        {
            tsbHistory.DropDownItems.Clear();
            if (history.Count > 0)
            {
                string[] tempHis=history.ToArray();
                foreach(string script in tempHis)
                {
                    tsbHistory.DropDownItems.Add(script);
                }
            }
        }

        private void tsbHistory_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            InjectScript(e.ClickedItem.Text);
        }

        private void rtbScript_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                ProcessScript(rtbScript.Text);        
            }
        }

        private void txtUrl_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                OpenUrl();
            }
        }

    }
}
